<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSharedMemory Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSharedMemory Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QSharedMemory class provides access to a shared memory
segment. <a href="#details">More...</a></p>

<p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qsharedmemory.html#AccessMode-enum">AccessMode</a></b> { ReadOnly, ReadWrite }</li><li><div class="fn" />enum <b><a href="qsharedmemory.html#SharedMemoryError-enum">SharedMemoryError</a></b> { NoError, PermissionDenied, InvalidSize, KeyError, ..., UnknownError }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsharedmemory.html#QSharedMemory">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" /><b><a href="qsharedmemory.html#QSharedMemory-2">__init__</a></b> (<i>self</i>, QString&#160;<i>key</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" />bool <b><a href="qsharedmemory.html#attach">attach</a></b> (<i>self</i>, AccessMode&#160;<i>mode</i>&#160;=&#160;QSharedMemory.ReadWrite)</li><li><div class="fn" />sip.voidptr <b><a href="qsharedmemory.html#constData">constData</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsharedmemory.html#create">create</a></b> (<i>self</i>, int&#160;<i>size</i>, AccessMode&#160;<i>mode</i>&#160;=&#160;QSharedMemory.ReadWrite)</li><li><div class="fn" />sip.voidptr <b><a href="qsharedmemory.html#data">data</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsharedmemory.html#detach">detach</a></b> (<i>self</i>)</li><li><div class="fn" />SharedMemoryError <b><a href="qsharedmemory.html#error">error</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsharedmemory.html#errorString">errorString</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsharedmemory.html#isAttached">isAttached</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsharedmemory.html#key">key</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsharedmemory.html#lock">lock</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsharedmemory.html#nativeKey">nativeKey</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsharedmemory.html#setKey">setKey</a></b> (<i>self</i>, QString&#160;<i>key</i>)</li><li><div class="fn" /><b><a href="qsharedmemory.html#setNativeKey">setNativeKey</a></b> (<i>self</i>, QString&#160;<i>key</i>)</li><li><div class="fn" />int <b><a href="qsharedmemory.html#size">size</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsharedmemory.html#unlock">unlock</a></b> (<i>self</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSharedMemory class provides access to a shared memory
segment.</p>
<p>QSharedMemory provides access to a shared memory segment by
multiple threads and processes. It also provides a way for a single
thread or process to lock the memory for exclusive access.</p>
<p>When using this class, be aware of the following platform
differences:</p>
<ul>
<li>Windows: QSharedMemory does not "own" the shared memory
segment. When all threads or processes that have an instance of
QSharedMemory attached to a particular shared memory segment have
either destroyed their instance of QSharedMemory or exited, the
Windows kernel releases the shared memory segment
automatically.</li>
<li>Unix: QSharedMemory "owns" the shared memory segment. When the
last thread or process that has an instance of QSharedMemory
attached to a particular shared memory segment detaches from the
segment by destroying its instance of QSharedMemory, the Unix
kernel release the shared memory segment. But if that last thread
or process crashes without running the QSharedMemory destructor,
the shared memory segment survives the crash.</li>
<li>QNX: Due to possible race conditions in the POSIX IPC
implementation, <a href="qsharedmemory.html#create">create</a>()
should be called prior to any <a href="qsharedmemory.html#attach">attach</a>() calls (even across
multiple threads).</li>
<li>HP-UX: Only one attach to a shared memory segment is allowed
per process. This means that QSharedMemory should not be used
across multiple threads in the same process in HP-UX.</li>
<li>Symbian: QSharedMemory does not "own" the shared memory
segment. When all threads or processes that have an instance of
QSharedMemory attached to a particular shared memory segment have
either destroyed their instance of QSharedMemory or exited, the
Symbian kernel releases the shared memory segment automatically.
Also, access to a shared memory segment cannot be limited to
read-only in Symbian.</li>
</ul>
<p>Remember to lock the shared memory with <a href="qsharedmemory.html#lock">lock</a>() before reading from or writing
to the shared memory, and remember to release the lock with
<a href="qsharedmemory.html#unlock">unlock</a>() after you are
done.</p>
<p>Unlike <a href="http://qt.nokia.com/products/add-on-products/catalog/4/Utilities/qtsharedmemory/">
QtSharedMemory</a>, QSharedMemory automatically destroys the shared
memory segment when the last instance of QSharedMemory is detached
from the segment, and no references to the segment remain. Do not
mix using <a href="http://qt.nokia.com/products/add-on-products/catalog/4/Utilities/qtsharedmemory/">
QtSharedMemory</a> and QSharedMemory. Port everything to
QSharedMemory.</p>
<p><b>Warning:</b> QSharedMemory changes the key in a Qt-specific
way, unless otherwise specified. Interoperation with non-Qt
applications is achieved by first creating a default shared memory
with <a href="qsharedmemory.html#QSharedMemory">QSharedMemory</a>()
and then setting a native key with <a href="qsharedmemory.html#setNativeKey">setNativeKey</a>(). When using
native keys, shared memory is not protected against multiple
accesses on it (e.g. unable to <a href="qsharedmemory.html#lock">lock</a>()) and a user-defined mechanism
should be used to achieve a such protection.</p>
<hr /><h2>Type Documentation</h2><h3 class="fn"><a name="AccessMode-enum" />QSharedMemory.AccessMode</h3><table class="valuelist">
<tr class="odd" valign="top">
<th class="tblConst">Constant</th>
<th class="tblval">Value</th>
<th class="tbldscr">Description</th>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.ReadOnly</tt></td>
<td class="topAlign"><tt>0</tt></td>
<td class="topAlign">The shared memory segment is read-only.
Writing to the shared memory segment is not allowed. An attempt to
write to a shared memory segment created with ReadOnly causes the
program to abort.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.ReadWrite</tt></td>
<td class="topAlign"><tt>1</tt></td>
<td class="topAlign">Reading and writing the shared memory segment
are both allowed.</td>
</tr>
</table>


<h3 class="fn"><a name="SharedMemoryError-enum" />QSharedMemory.SharedMemoryError</h3><table class="valuelist">
<tr class="odd" valign="top">
<th class="tblConst">Constant</th>
<th class="tblval">Value</th>
<th class="tbldscr">Description</th>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.NoError</tt></td>
<td class="topAlign"><tt>0</tt></td>
<td class="topAlign">No error occurred.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.PermissionDenied</tt></td>
<td class="topAlign"><tt>1</tt></td>
<td class="topAlign">The operation failed because the caller didn't
have the required permissions.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.InvalidSize</tt></td>
<td class="topAlign"><tt>2</tt></td>
<td class="topAlign">A create operation failed because the
requested size was invalid.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.KeyError</tt></td>
<td class="topAlign"><tt>3</tt></td>
<td class="topAlign">The operation failed because of an invalid
key.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.AlreadyExists</tt></td>
<td class="topAlign"><tt>4</tt></td>
<td class="topAlign">A <a href="qsharedmemory.html#create">create</a>() operation failed because a
shared memory segment with the specified key already existed.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.NotFound</tt></td>
<td class="topAlign"><tt>5</tt></td>
<td class="topAlign">An <a href="qsharedmemory.html#attach">attach</a>() failed because a shared
memory segment with the specified key could not be found.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.LockError</tt></td>
<td class="topAlign"><tt>6</tt></td>
<td class="topAlign">The attempt to <a href="qsharedmemory.html#lock">lock</a>() the shared memory segment
failed because <a href="qsharedmemory.html#create">create</a>() or
<a href="qsharedmemory.html#attach">attach</a>() failed and
returned false, or because a system error occurred in <a href="qsystemsemaphore.html#acquire">QSystemSemaphore.acquire</a>().</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.OutOfResources</tt></td>
<td class="topAlign"><tt>7</tt></td>
<td class="topAlign">A <a href="qsharedmemory.html#create">create</a>() operation failed because
there was not enough memory available to fill the request.</td>
</tr>
<tr>
<td class="topAlign"><tt>QSharedMemory.UnknownError</tt></td>
<td class="topAlign"><tt>8</tt></td>
<td class="topAlign">Something else happened and it was bad.</td>
</tr>
</table>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSharedMemory" />QSharedMemory.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a shared memory object with the given <i>parent</i>
and with its key set to <i>key</i>. Because its key is set, its
<a href="qsharedmemory.html#create">create</a>() and <a href="qsharedmemory.html#attach">attach</a>() functions can be
called.</p>
<p><b>See also</b> <a href="qsharedmemory.html#setKey">setKey</a>(), <a href="qsharedmemory.html#create">create</a>(), and <a href="qsharedmemory.html#attach">attach</a>().</p>


<h3 class="fn"><a name="QSharedMemory-2" />QSharedMemory.__init__ (<i>self</i>, QString&#160;<i>key</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>This function overloads <a href="qsharedmemory.html#QSharedMemory">QSharedMemory</a>().</p>
<p>Constructs a shared memory object with the given <i>parent</i>.
The shared memory object's key is not set by the constructor, so
the shared memory object does not have an underlying shared memory
segment attached. The key must be set with <a href="qsharedmemory.html#setKey">setKey</a>() or <a href="qsharedmemory.html#setNativeKey">setNativeKey</a>() before
<a href="qsharedmemory.html#create">create</a>() or <a href="qsharedmemory.html#attach">attach</a>() can be used.</p>
<p><b>See also</b> <a href="qsharedmemory.html#setKey">setKey</a>().</p>


<h3 class="fn"><a name="attach" />bool QSharedMemory.attach (<i>self</i>, <a href="qsharedmemory.html#AccessMode-enum">AccessMode</a>&#160;<i>mode</i>&#160;=&#160;QSharedMemory.ReadWrite)</h3><p>Attempts to attach the process to the shared memory segment
identified by the key that was passed to the constructor or to a
call to <a href="qsharedmemory.html#setKey">setKey</a>() or
<a href="qsharedmemory.html#setNativeKey">setNativeKey</a>(). The
access <i>mode</i> is <a href="qsharedmemory.html#AccessMode-enum">ReadWrite</a> by default. It
can also be <a href="qsharedmemory.html#AccessMode-enum">ReadOnly</a>. Returns true if
the attach operation is successful. If false is returned, call
<a href="qsharedmemory.html#error">error</a>() to determine which
error occurred. After attaching the shared memory segment, a
pointer to the shared memory can be obtained by calling <a href="qsharedmemory.html#data">data</a>().</p>
<p><b>See also</b> <a href="qsharedmemory.html#isAttached">isAttached</a>(), <a href="qsharedmemory.html#detach">detach</a>(), and <a href="qsharedmemory.html#create">create</a>().</p>


<h3 class="fn"><a name="constData" />sip.voidptr QSharedMemory.constData (<i>self</i>)</h3><p>Returns a const pointer to the contents of the shared memory
segment, if one is attached. Otherwise it returns null. Remember to
lock the shared memory with <a href="qsharedmemory.html#lock">lock</a>() before reading from or writing
to the shared memory, and remember to release the lock with
<a href="qsharedmemory.html#unlock">unlock</a>() after you are
done.</p>
<p><b>See also</b> <a href="qsharedmemory.html#attach">attach</a>()
and <a href="qsharedmemory.html#create">create</a>().</p>


<h3 class="fn"><a name="create" />bool QSharedMemory.create (<i>self</i>, int&#160;<i>size</i>, <a href="qsharedmemory.html#AccessMode-enum">AccessMode</a>&#160;<i>mode</i>&#160;=&#160;QSharedMemory.ReadWrite)</h3><p>Creates a shared memory segment of <i>size</i> bytes with the
key passed to the constructor, set with <a href="qsharedmemory.html#setKey">setKey</a>() or set with <a href="qsharedmemory.html#setNativeKey">setNativeKey</a>(), then attaches
to the new shared memory segment with the given access <i>mode</i>
and returns <tt>true</tt>. If a shared memory segment identified by
the key already exists, the attach operation is not performed and
<tt>false</tt> is returned. When the return value is
<tt>false</tt>, call <a href="qsharedmemory.html#error">error</a>()
to determine which error occurred.</p>
<p><b>See also</b> <a href="qsharedmemory.html#error">error</a>().</p>


<h3 class="fn"><a name="data" />sip.voidptr QSharedMemory.data (<i>self</i>)</h3><p>Returns a pointer to the contents of the shared memory segment,
if one is attached. Otherwise it returns null. Remember to lock the
shared memory with <a href="qsharedmemory.html#lock">lock</a>()
before reading from or writing to the shared memory, and remember
to release the lock with <a href="qsharedmemory.html#unlock">unlock</a>() after you are done.</p>
<p><b>See also</b> <a href="qsharedmemory.html#attach">attach</a>().</p>


<h3 class="fn"><a name="detach" />bool QSharedMemory.detach (<i>self</i>)</h3><p>Detaches the process from the shared memory segment. If this was
the last process attached to the shared memory segment, then the
shared memory segment is released by the system, i.e., the contents
are destroyed. The function returns true if it detaches the shared
memory segment. If it returns false, it usually means the segment
either isn't attached, or it is locked by another process.</p>
<p><b>See also</b> <a href="qsharedmemory.html#attach">attach</a>()
and <a href="qsharedmemory.html#isAttached">isAttached</a>().</p>


<h3 class="fn"><a name="error" /><a href="qsharedmemory.html#SharedMemoryError-enum">SharedMemoryError</a> QSharedMemory.error (<i>self</i>)</h3><p>Returns a value indicating whether an error occurred, and, if
so, which error it was.</p>
<p><b>See also</b> <a href="qsharedmemory.html#errorString">errorString</a>().</p>


<h3 class="fn"><a name="errorString" />QString QSharedMemory.errorString (<i>self</i>)</h3><p>Returns a text description of the last error that occurred. If
<a href="qsharedmemory.html#error">error</a>() returns an <a href="qsharedmemory.html#SharedMemoryError-enum">error value</a>, call
this function to get a text string that describes the error.</p>
<p><b>See also</b> <a href="qsharedmemory.html#error">error</a>().</p>


<h3 class="fn"><a name="isAttached" />bool QSharedMemory.isAttached (<i>self</i>)</h3><p>Returns true if this process is attached to the shared memory
segment.</p>
<p><b>See also</b> <a href="qsharedmemory.html#attach">attach</a>()
and <a href="qsharedmemory.html#detach">detach</a>().</p>


<h3 class="fn"><a name="key" />QString QSharedMemory.key (<i>self</i>)</h3><p>Returns the key assigned with <a href="qsharedmemory.html#setKey">setKey</a>() to this shared memory, or
a null key if no key has been assigned, or if the segment is using
a <a href="qsharedmemory.html#nativeKey">nativeKey</a>(). The key
is the identifier used by Qt applications to identify the shared
memory segment.</p>
<p>You can find the native, platform specific, key used by the
operating system by calling <a href="qsharedmemory.html#nativeKey">nativeKey</a>().</p>
<p><b>See also</b> <a href="qsharedmemory.html#setKey">setKey</a>()
and <a href="qsharedmemory.html#setNativeKey">setNativeKey</a>().</p>


<h3 class="fn"><a name="lock" />bool QSharedMemory.lock (<i>self</i>)</h3><p>This is a semaphore that locks the shared memory segment for
access by this process and returns true. If another process has
locked the segment, this function blocks until the lock is
released. Then it acquires the lock and returns true. If this
function returns false, it means that you have ignored a false
return from <a href="qsharedmemory.html#create">create</a>() or
<a href="qsharedmemory.html#attach">attach</a>(), that you have set
the key with <a href="qsharedmemory.html#setNativeKey">setNativeKey</a>() or that
<a href="qsystemsemaphore.html#acquire">QSystemSemaphore.acquire</a>()
failed due to an unknown system error.</p>
<p><b>See also</b> <a href="qsharedmemory.html#unlock">unlock</a>(), <a href="qsharedmemory.html#data">data</a>(), and <a href="qsystemsemaphore.html#acquire">QSystemSemaphore.acquire</a>().</p>


<h3 class="fn"><a name="nativeKey" />QString QSharedMemory.nativeKey (<i>self</i>)</h3><p>Returns the native, platform specific, key for this shared
memory object. The native key is the identifier used by the
operating system to identify the shared memory segment.</p>
<p>You can use the native key to access shared memory segments that
have not been created by Qt, or to grant shared memory access to
non-Qt applications.</p>
<p>This function was introduced in Qt 4.8.</p>
<p><b>See also</b> <a href="qsharedmemory.html#setKey">setKey</a>()
and <a href="qsharedmemory.html#setNativeKey">setNativeKey</a>().</p>


<h3 class="fn"><a name="setKey" />QSharedMemory.setKey (<i>self</i>, QString&#160;<i>key</i>)</h3><p>Sets the platform independent <i>key</i> for this shared memory
object. If <i>key</i> is the same as the current key, the function
returns without doing anything.</p>
<p>You can call <a href="qsharedmemory.html#key">key</a>() to
retrieve the platform independent key. Internally, <a href="qsharedmemory.html">QSharedMemory</a> converts this key into a
platform specific key. If you instead call <a href="qsharedmemory.html#nativeKey">nativeKey</a>(), you will get the
platform specific, converted key.</p>
<p>If the shared memory object is attached to an underlying shared
memory segment, it will <a href="qsharedmemory.html#detach">detach</a> from it before setting the
new key. This function does not do an <a href="qsharedmemory.html#attach">attach</a>().</p>
<p><b>See also</b> <a href="qsharedmemory.html#key">key</a>(),
<a href="qsharedmemory.html#nativeKey">nativeKey</a>(), and
<a href="qsharedmemory.html#isAttached">isAttached</a>().</p>


<h3 class="fn"><a name="setNativeKey" />QSharedMemory.setNativeKey (<i>self</i>, QString&#160;<i>key</i>)</h3><p>Sets the native, platform specific, <i>key</i> for this shared
memory object. If <i>key</i> is the same as the current native key,
the function returns without doing anything. If all you want is to
assign a key to a segment, you should call <a href="qsharedmemory.html#setKey">setKey</a>() instead.</p>
<p>You can call <a href="qsharedmemory.html#nativeKey">nativeKey</a>() to retrieve the
native key. If a native key has been assigned, calling <a href="qsharedmemory.html#key">key</a>() will return a null string.</p>
<p>If the shared memory object is attached to an underlying shared
memory segment, it will <a href="qsharedmemory.html#detach">detach</a> from it before setting the
new key. This function does not do an <a href="qsharedmemory.html#attach">attach</a>().</p>
<p>The application will not be portable if you set a native
key.</p>
<p>This function was introduced in Qt 4.8.</p>
<p><b>See also</b> <a href="qsharedmemory.html#nativeKey">nativeKey</a>(), <a href="qsharedmemory.html#key">key</a>(), and <a href="qsharedmemory.html#isAttached">isAttached</a>().</p>


<h3 class="fn"><a name="size" />int QSharedMemory.size (<i>self</i>)</h3><p>Returns the size of the attached shared memory segment. If no
shared memory segment is attached, 0 is returned.</p>
<p><b>See also</b> <a href="qsharedmemory.html#create">create</a>()
and <a href="qsharedmemory.html#attach">attach</a>().</p>


<h3 class="fn"><a name="unlock" />bool QSharedMemory.unlock (<i>self</i>)</h3><p>Releases the lock on the shared memory segment and returns true,
if the lock is currently held by this process. If the segment is
not locked, or if the lock is held by another process, nothing
happens and false is returned.</p>
<p><b>See also</b> <a href="qsharedmemory.html#lock">lock</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;snapshot-4.9.5-9eb6aac99275 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt&#160;4.8.3</td></tr></table></div></address></body></html>